package com.demo.controller;

import com.demo.pojo.User;
import com.demo.service.UserService;
import com.demo.utils.ImageUtil;
import com.demo.utils.SaltUtil;
import org.apache.ibatis.javassist.ClassPath;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.image.RenderedImage;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

/**
 * @author chenyuan
 * @date 2020/9/19 10:10
 */
@Controller
public class IndexController {
    @Autowired
    UserService userService;

    //退出登录

    @RequestMapping("/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }
    @RequestMapping("index")
    public String index(){
        return "index";
    }

//    @RequestMapping("/getImage")
//    public void getImage(){
//        ImageUtil.getCheckCodePic();
//    }

    //登录的方法

    @RequestMapping("/login")
    public String login(String username, String password, Model model) {
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();

        //封装用户的登录数据,获得令牌
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        //登录 及 异常处理
        try {
            //用户登录
            subject.login(token);
            return "redirect:/index";
        } catch (UnknownAccountException uae) {
            //如果用户名不存在
            System.out.println("用户名不存在");
            model.addAttribute("exception", "用户名不存在");
            return "login";
        } catch (IncorrectCredentialsException ice) {
            //如果密码错误
            System.out.println("密码错误");
            model.addAttribute("exception", "密码错误");
            return "login";
        }
    }
    @RequestMapping("/tologin")
    public String toLogin(Map map, HttpServletRequest request) throws Exception {
        map.putAll(ImageUtil.generateCodeAndPic());
        //创建文件输出流对象
        return "login";
    }

    //注册

    @RequestMapping("/toregister")
    public String toRegister(){
        return "register";
    }

    @RequestMapping("/register")
    public String register(String username, String password){
        User user = new User();
        user.setName(username);
        user.setPassword(password);
        try {
            userService.register(user);
            return "login";
        }catch (Exception e){
            e.printStackTrace();
            return "redirect:/toregister";
        }
    }

// 1

    @RequestMapping("/l1/{id}")
    public String l1(@PathVariable("id") int id){
        return "l1/l1-"+id;
    }
//  2

    @RequestMapping("/l2/{id}")
    public String l2(@PathVariable("id") int id){
        return "l2/l2-"+id;
    }

//  3

    @RequestMapping("/l3/{id}")
    //@RequiresRoles(value = {"admin","user"})  // 判断角色
    //@RequiresPermissions("user:/l3/*:*") // 判断权限字符串

    public String l3(@PathVariable("id") int id, Model model){
        return "l3/l3-"+id;
    }

//    public String l3(@PathVariable("id") int id, Model model){
//        // 获取对象
//        Subject subject = SecurityUtils.getSubject();
//        if (subject.hasRole("admin")){
//            return "l3/l3-"+id;
//        }else {
//            model.addAttribute("msg", "没有权限");
//            return "index";
//        }
//    }
}
